package com.example.controller;

import com.example.common.Result;
import com.example.entity.Address;
import com.example.entity.Admin;
import com.example.service.AdminService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * 管理员前端操作接口
 **/
@RestController
@RequestMapping("/admin")
@Api(value = "管理员", tags = {"管理员用户的增删改查"})
public class AdminController {

    @Resource
    private AdminService adminService;

    /**
     * 新增
     */
    @ApiOperation(value = "增加管理员", notes = "返回是否成功")
    @PostMapping("/add")
    public Result add(@RequestBody Admin admin) {
        adminService.add(admin);
        return Result.success();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除管理员", notes = "返回是否成功")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "id",
                    value = "要删除的管理员id,如'1'"
            )
    })
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        adminService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "批量删除管理员", notes = "返回是否成功")
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        adminService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新管理员信息", notes = "返回是否成功")
    @PutMapping("/update")
    public Result updateById(@RequestBody Admin admin) {
        adminService.updateById(admin);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "通过管理员id查询信息", notes = "返回Models中的管理员用户类型")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "id",
                    value = "要查询的管理员用户数据库id,如'1'"
            )
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Admin.class,reference = "Admin")
    })
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Admin admin = adminService.selectById(id);
        return Result.success(admin);
    }

    /**
     * 查询所有
     */
    @ApiOperation(value = "查询所有符合特征的管理员信息", notes = "返回Models中的管理员用户类型的数组")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Admin.class,reference = "Admin")
    })
    @GetMapping("/selectAll")
    public Result selectAll(Admin admin ) {
        List<Admin> list = adminService.selectAll(admin);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询所有符合特征的管理员信息", notes = "返回Models中的管理员户类型的分页数组")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "pageNum",
                    value = "要查询的页码,默认为'1'"
            ),
            @ApiImplicitParam(
                    name = "pageSize",
                    value = "每页尺寸,默认为'10'"
            )
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Admin.class,reference = "Admin")
    })
    @GetMapping("/selectPage")
    public Result selectPage(Admin admin,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Admin> page = adminService.selectPage(admin, pageNum, pageSize);
        return Result.success(page);
    }

}